import { SET_USER_FETCH_STATUS, SET_USER_INFO } from '@/actionTypes';

import http from '@/api/http';

/**
 * 设置用户信息请求状态
 * 
 * @param {*} status
 * @returns   
 */
export const setUserFetchStatus = (status) => {
  return {
    type: SET_USER_FETCH_STATUS,
    status,
  };
};

/**
 * 设置用户信息
 * 
 * @export
 * @param {any} userInfo 
 * @returns 
 */
export const setUserInfo = (userInfo) => {
  return {
    type: SET_USER_INFO,
    userInfo,
  };
};

/**
 * 获取用户信息
 * 
 * @param {string} userName 
 */
export const getUserInfo = (userName) => {
  return (dispatch) => {
    // 设置正在获取数据中
    dispatch(setUserFetchStatus('fetching'));

    const url = `https://api.github.com/users/${userName}`;
    setTimeout(() => {
      http.get(url, {}, (res) => {
        dispatch(setUserFetchStatus('success'));
        dispatch(setUserInfo(res));
      }, (err) => {
        console.log('err', err);
        dispatch(setUserFetchStatus('error'));
      });
    }, 2000);
  };
};
